package 城市群数量;

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param m int整型ArrayList<ArrayList<>>
     * @return int整型
     */
    static int N = 210;
    static int[] p = new int[N];

    public int citys(ArrayList<ArrayList<Integer>> m) {
        int n = m.size();
        for (int i = 0; i < n; i++) {
            p[i] = i;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (m.get(i).get(j) == 1) {
                    p[find(i)] = find(j);
                }
            }
        }
        int ret = 0;
        for (int i = 0; i < n; i++) {
            if (p[i] == i) {
                ret++;
            }
        }
        return ret;
    }

    public static int find(int x) {
        if (x != p[x]) {
            p[x] = find(p[x]);
        }
        return p[x];
    }
}